package testiranjeSoftvera.phage;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import testiranjeSoftvera.GuiElements.GameButton;
import testiranjeSoftvera.GuiElements.MenuBar;
import testiranjeSoftvera.actions.ActionManager;
import testiranjeSoftvera.virus.AbstractVirus;
import testiranjeSoftvera.virus.SlowVirus;
import testiranjeSoftvera.virus.SpeedyVirus;
import testiranjeSoftvera.virus.WarriorVirus;

public class PhageCore extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8334505778677076639L;

	private static PhageCore instance;

	private ActionManager accManager;

	private JPanel mainPanel;
	private JButton virusEditorButton;
	private JButton mapEditorButton;
	private JButton startGameButton;
	private JPanel gamePanel;
	private JPanel restPanel;
	private MenuBar testMenuBar;

	// Viruses
	private AbstractVirus warriorVirus;
	private AbstractVirus speedyVirus;
	private AbstractVirus slowVirus;
	private ArrayList<AbstractVirus> virusesList;

	private PhageCore() {

		// Create instance of each virus
		warriorVirus = new WarriorVirus();
		speedyVirus = new SpeedyVirus();
		slowVirus = new SlowVirus();

		// Set values
		setTitle("Testiranje softvera -  PhageWars");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// Set location on the screen
		Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit()
				.getScreenSize());
		setBounds((int) screenSize.getWidth() / 2 - 250,
				(int) screenSize.getHeight() / 2 - 150, 500, 300);

		// Create Action Manager
		accManager = new ActionManager();

	}

	public static PhageCore getInstance() {
		if (instance == null) {
			instance = new PhageCore();
			instance.init();
		}
		return instance;
	}

	public void init() {
		testMenuBar = new MenuBar();
		setJMenuBar(testMenuBar);
		setContentPane(getMainPanel());

		// pack();

		virusesList = new ArrayList<>();
		virusesList.add(speedyVirus);
		virusesList.add(warriorVirus);
		virusesList.add(slowVirus);

	}

	public ArrayList<AbstractVirus> getVirusesList() {
		return virusesList;
	}

	public JPanel getMainPanel() {
		if (mainPanel == null) {

			// Add buttons on main
			mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
			mainPanel.add(getStartGameButton());
			mainPanel.add(getVirusEditorButton());
			mainPanel.add(getMapEditorButton());
		}
		return mainPanel;
	}

	public JButton getVirusEditorButton() {
		if (virusEditorButton == null) {
			virusEditorButton = new GameButton("Virus Editor");
			virusEditorButton.addActionListener(accManager.getVedAction());
			virusEditorButton.setIcon(null);
		}
		return virusEditorButton;
	}

	public JButton getStartGameButton() {
		if (startGameButton == null) {
			startGameButton = new GameButton("Start");
			startGameButton.addActionListener(accManager.getStartgameAction());
			startGameButton.setIcon(null);
		}
		return startGameButton;
	}

	public JButton getMapEditorButton() {
		if (mapEditorButton == null) {
			mapEditorButton = new GameButton("Map Editor");
			mapEditorButton.addActionListener(accManager.getTstDummyAction());
			mapEditorButton.setIcon(null);
		}
		return mapEditorButton;
	}

	public JPanel getGamePanel() {
		if (gamePanel == null) {
			gamePanel = new JPanel();
			JLabel labela = new JLabel("Ovde ce se igrati nesto");
			gamePanel.setBorder(BorderFactory
					.createTitledBorder("GamePanel - Phage Wars"));
			gamePanel.add(labela);
		}
		return gamePanel;
	}

	public JPanel getRestPanel() {

		if (restPanel == null) {
			restPanel = new JPanel();

		}
		return restPanel;
	}

	public ActionManager getAccManager() {
		return accManager;
	}

	public AbstractVirus getWarriorWirus() {
		return warriorVirus;
	}

	public AbstractVirus getSlowVirus() {
		return slowVirus;
	}

	public AbstractVirus getSpeedyVirus() {
		return speedyVirus;
	}

}
